package com.aaron.startup;

import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import com.aaron.socket.utils.ConfigFilePath;

/**
 * 
 * @类名称：SpringStartup
 * @类描述：spring特性开机启动
 * @创建人：aaron
 * @修改人：
 * @修改时间：2014年5月11日 下午9:51:45
 * @修改备注：
 * @version:
 */
public class SpringStartup
{

    /**
     * 日志对象
     */
    private final static Logger logger = Logger.getLogger(SpringStartup.class
            .getName());

    /**
     * spring 配置文件
     */
    private static final String configName = "applicationContext.xml";

    private static BeanFactory factory = null;

    private static ApplicationContext context = null;

    private static SpringStartup startup = new SpringStartup();

    /**
     * 初始化读取配置文件
     */
    static
    {
        logger.log(Level.INFO, "开始初始化spring");

        String[] pathList = ConfigFilePath.getSpringConfPath(configName);
        logger.log(Level.INFO, Arrays.toString(pathList));

        if (null == pathList || 0 == pathList.length)
        {

            logger.log(Level.OFF, "配置文件加载出错，系统关闭");
            System.exit(0);
        } else
        {

            context = new FileSystemXmlApplicationContext(pathList);

            if (null == context)
            {
                logger.log(Level.OFF, "配置文件加载出错，系统关闭");
                System.exit(0);
            } else
            {
                factory = context;
            }

        }

    }

    public static SpringStartup getIntance()
    {
        return startup;
    }

    private SpringStartup()
    {

    }

    /**
     * 
     * @方法描述：获取jdbc对象
     * @方法名：loadJdbcTemplate
     * @参数：@return
     * @返回：JdbcTemplate
     * @exception
     * @since
     */
    public JdbcTemplate loadJdbcTemplate()
    {
        return (JdbcTemplate) factory.getBean("jdbcTemplate");
    }

    public DataSourceTransactionManager loadTransactionManager()
    {
        return (DataSourceTransactionManager) factory
                .getBean("transactionManager");
    }

}
